﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;
using VehicleMaintenance.Comm;

namespace VehicleMaintenance.BaseInfo
{
	public partial class CustomerForm : AddOrEditBaseForm
	{

		/// <summary>
		/// 记录当前客户ID
		/// </summary>
		private int customerID = 0;

		public CustomerForm()
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.Customer));
			InitializeSepcial();
		}

		public CustomerForm(int id)
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.Customer));
			InitializeSepcial();

			customerID = id;
			this.state = State.Edit;
		}

		private void CustomerForm_Load(object sender, EventArgs e)
		{
			try
			{
				if (this.state == State.Edit)
				{
					CarDBContext db = new CarDBContext();

					Customer customer = db.Customer.Include("CustomerType").First(p => p.CustomerID == this.customerID);
					if (customer != null)
					{
						ucFormInput1.SetValues(customer);

						ucFormInput1.GetControlByName("TypeID").Text = customer.CustomerType.Name;
					}
				}
				else
				{
					this.ClearData(this.panel1);
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				CarDBContext db = new CarDBContext();

				if (this.state == State.Edit)
				{
					Customer customer = db.Customer.First(p => p.CustomerID == this.customerID);
					ucFormInput1.GetValues(customer);

					db.SaveChanges();

					this.ShowInfo("保存成功！");
				}
				else
				{
					//查询员工编码是否重复
					string strCode = ucFormInput1.GetControlByName("Code").Text;
					if (db.Customer.SingleOrDefault(p => p.Code == strCode) != null)
					{
						this.ShowInfo("员工编码已经存在");
						//tabPanel.Controls["Code"]();
						ucFormInput1.GetControlByName("Code").Focus();
						return;
					}
					var customer = db.Customer.Create();
					ucFormInput1.GetValues(customer);


					db.Customer.Add(customer);
					db.SaveChanges();

					if (MessageBox.Show(this, "保存成功，是否继续添加？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
					{
						this.ClearData(this.panel1);
						ucFormInput1.GetControlByName("Code").Focus();
					}
					else
					{
						this.DialogResult = System.Windows.Forms.DialogResult.OK;
					}
				}

			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		private void InitializeSepcial()
		{
			try
			{
				ucFormInput1.GetControlByName("TypeID").Click += new EventHandler(type_Click);
				ucFormInput1.GetControlByName("TypeID").BackColor = Color.LightGray;
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		private void type_Click(object sender, EventArgs e)
		{
			try
			{
				TextBox txt = (sender as TextBox);

				CustomerTypeForm form = new CustomerTypeForm();
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.CusType.Name;
					txt.Tag = form.CusType.TypeID;
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

	}
}


